<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\business\Product;
use app\lib\Show;
use think\Exception;
use think\facade\Log;
use think\response\Json;

class Home extends Base {

    public function getRotationChart() : Json {
        $bannerList = [];
        try{
            $data = [
                "where" => ["is_home_recommend" => 1, "big_img" => "<>"],
                "fields" => "id, big_img, sku_id",
            ];
            $result = (new Product())->pageList(1, $data);
            $result = !empty($result) && isset($result["data"]) ? $result["data"] : [];
            $bannerList = [];
            foreach ($result as $item) {
                $bannerList[] = [
                    "type" => "product",
                    "id" => $item["id"],
                    "image" => $item["big_img"],
                    "sku_id" => $item["sku_id"]
                ];
            }
            unset($result);
        }catch (Exception $e){
            Log::error($e->getMessage());
            return Show::fail("获取数据失败");
        }

        return Show::success("ok", $bannerList);
    }

    public function categoryGoodsRecommend() : Json {
        $productList = [];
        try{
            $productList = (new \app\api\business\Product())
                ->categoryGoodsRecommend(6, "id, title, category_id,
                 category_path_id, price, recommend_img, sku_id");
        }catch (Exception $e){
            Log::error($e->getMessage());
            return Show::fail("获取数据失败");
        }
        return Show::success("ok", $productList);
    }
}